/**
 * basicOverview.js
 * 基础概况reducer
 *
 * created by qiuwei
 * version 1.0.0
 * 2016年03月09日
 */

import { BASIC, CHART, } from '../../../constants/basicOverview';
import { CITY, COUNTY, SCHOOL } from '../../../constants/area';
import moment from 'moment';
import { DATE_FORMMATTER } from '../../../constants/datetime';
import merge from 'lodash/merge';
import cloneDeep from 'lodash/cloneDeep';
import assign from 'lodash/assign'
import {
	SWITCH_VIEW,
	GET_BASIC_TABLE_REQUEST,
	GET_BASIC_TABLE_SUCCESS,
	GET_BASIC_TABLE_FAILURE,
	GET_ACTIVE_TABLE_REQUEST,
	GET_ACTIVE_TABLE_SUCCESS,
	GET_ACTIVE_TABLE_FAILURE,
	GET_CHARTS_BASIC_REQUEST,
	GET_CHARTS_BASIC_SUCCESS,
	GET_CHARTS_BASIC_FAILURE,
	SWITCH_CHARTS_BASIC,
	GET_CHARTS_ACTIVE_REQUEST,
	GET_CHARTS_ACTIVE_SUCCESS,
	GET_CHARTS_ACTIVE_FAILURE,
	GET_DETAIL_REQUEST,
	GET_DETAIL_SUCCESS,
	GET_DETAIL_FAILURE,
	SELECT_BASIC_DETAIL_PAGEINDEX,
	SELECT_BASIC_DETAIL_CURRENT_PAGEINDEX,
	GET_BASIC_DETAIL_REQUEST,
	GET_BASIC_DETAIL_SUCCESS,
	GET_BASIC_DETAIL_FAILURE,
	SELECT_ACTIVE_DETAIL_PAGEINDEX,
	SELECT_ACTIVE_DETAIL_CURRENT_PAGEINDEX,
	GET_ACTIVE_DETAIL_REQUEST,
	GET_ACTIVE_DETAIL_SUCCESS,
	GET_ACTIVE_DETAIL_FAILURE,
	RESET
} from '../actions/basicOverview';

let now = moment();
let lastMonth = moment().subtract(1, 'M');

/**
 * 初始基础概况页面状态
 * @type {Object}
 */
export const INITIALIZE_STATE = {
	// 筛选条件
	// 提示消息
	message:'',
	// 默认二级视图(基础概况,活跃热力图)
	view: BASIC,
	// 各级地域名数字化教育基础概况
	basicTable: {
		isFetch: false,
		// 截止日期
		endDate: '',
		// 数据表
		dataSource: [[]]
	},
	// [各级地域名]师生活跃概况
	activeTable: {
		isFetch: false,
		// 数据表
		dataSource: [[]]
	},
	charts: {
		// [各级地域名]累计增长趋势
		/**
		 * 累计学生，累计教师，累计学校合并请求
		 * @type {String}
		 * @param {object} [[student],[teacher],[school]] dataSource
		 */
		basic: {
			isFetch: false,
			dataSource: [[[],[]],[[],[]],[[],[]]]
		},
		
		// [各级地域名]活跃热力图
		active: {
			isFetch: false,
			geo: [{}]
		}
	},
	// 数据详情
	
	detail: {
		basic:{
			isFetch: false,
			//总页数
			pageIndex:0,
			//当前页
			currentPageIndex:1,
			dataSource: [[]]
		},
		active: {
			isFetch: false,
			//总页数
			pageIndex:0,
			//当前页
			currentPageIndex:1,
			dataSource: [[]]
		}
	}
};

/**
 * 处理基础
 * @param  {[type]} state  [description]
 * @param  {Object} action [description]
 * @return {[type]}        [description]
 */
export default function basicOverview(state = INITIALIZE_STATE, action = {}) {
	let { type,...others } = action;

	switch(type) {
		case SWITCH_VIEW:
			// TODO: 切换基础视图和活跃热力图
			{
				let { view } = others.payload;
				let newState = Object.assign({} , state , {
					view
				});
				return newState;
			};
		/**
		 * 切换基础视图列表
		 * @type {[type]}
		 */
		case GET_BASIC_TABLE_REQUEST:
			// TODO: 基础视图列表切换请求
			{
				let newState = merge({},state,{
					basicTable:{
						isFetch:true
					}
				});
				return newState;
			};
		case GET_BASIC_TABLE_SUCCESS:
			// TODO: 成功切换基础视图列表，停止通讯
			{
				let { dataSource } = others.payload;
				
				
				let basicTable = cloneDeep(state.basicTable);
					basicTable = Object.assign({},basicTable,{
						dataSource,
						
						isFetch:false
					})

				let newState = Object.assign({},state,{ basicTable });

				return newState;
			};
		case GET_BASIC_TABLE_FAILURE:
			// TODO: 切换基础视图列表失败，停止通讯
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					basicTable:{
						isFetch:false
					},
					message:errorMessage
				});
				return newState;
			};
		/**
		 * 切换师生活跃概况表
		 * @type {[type]}
		 */
		case GET_ACTIVE_TABLE_REQUEST:
			// TODO: 切换师生活跃概况表，请求通讯
			{
				let newState = merge({},state,{
					activeTable:{
						isFetch:true
					}
				});
				return newState;
			};
		case GET_ACTIVE_TABLE_SUCCESS:
			// TODO: 师生活跃概况表请求成功，获取数据，停止通讯
			{	let { dataSource } = others.payload;
				let activeTable = cloneDeep(state.activeTable);
					activeTable.dataSource = dataSource;
					activeTable.isFetch=false;
				let newState = Object.assign({},state,{ activeTable });
				return newState;
			};
		case GET_ACTIVE_TABLE_FAILURE:
			// TODO: 师生活跃概况表请求失败，返回错误消息，停止通讯
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					activeTable:{
						isFetch:false
					},
					message:errorMessage
				});
				return newState;
			};
		/**
		 * 切换学生累计增长视图
		 * @type {[type]}
		 */
		case GET_CHARTS_BASIC_REQUEST:
			// TODO: 请求学生累计增长视图
			{	
				let newState = merge({},state,{
					charts:{
						basic:{
							isFetch:true
						}
					}
				});
				return newState;
			};
		case GET_CHARTS_BASIC_SUCCESS:
			// TODO: 成功获取累计增长视图，停止通讯
			{	let { dataSource } = others.payload

				let newBasic=cloneDeep(state.charts.basic);
				let basic=Object.assign({},newBasic,{
							isFetch:false,
							dataSource
					})
				let newCharts=cloneDeep(state.charts);
				let charts=Object.assign({},newCharts,{
						basic
					})
				let newState = Object.assign({},state,{
						charts
					});
				return newState;
			};
		case GET_CHARTS_BASIC_FAILURE:
			// TODO: 学生累计增长视图获取失败，停止通讯
			{
				let { errorMessage } = others.payload;
				let charts = cloneDeep(state.charts);

				charts.basic.dataSource = [[[],[]],[[],[]],[[],[]]];
				charts.basic.isFetch = false;

				let newState = assign({},state, {
					charts,
					message:errorMessage
				});
				return newState;
			};
		/**
		 * 切换图表数据
		 */
		case SWITCH_CHARTS_BASIC:
			{
				let { pointer } = others.payload;
				let newState = merge({},state,{
					charts:{
						basic:{
							pointer
						}
					}
				})
				return newState
			}
		/**
		 * [切换活跃热力图 description]
		 * @type {[type]}
		 */
		case GET_CHARTS_ACTIVE_REQUEST:
			// TODO: 请求活跃热力图
			{
				let newState = merge({},state,{
					charts:{
						active:{
							isFetch:true
						}
					}
				});
				return newState;
			};
		case GET_CHARTS_ACTIVE_SUCCESS:
			// TODO: 活跃热力图请求成功，停止通讯
			{
				let { geo } = others.payload;
				let newActive = cloneDeep(state.charts.active);
				let	active = Object.assign({},newActive,{
						geo
					});
				let newCharts = cloneDeep(state.charts);
				let	charts = Object.assign({},newCharts,{
						active
					})
				let newState = Object.assign({},state,{
						charts
					});
				return newState;
			};
		case GET_CHARTS_ACTIVE_FAILURE:
			// TODO: 活跃热力图请求失败，停止通讯
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					charts:{
						active:{
							isFetch:false
						}
					},
					message:errorMessage
				});
				return newState;
			};
		/**
		 * [异步获取数据详情 description]
		 * @type {[type]}
		 */
		case GET_BASIC_DETAIL_REQUEST:
			// TODO: 请求数据详情
			{
				let newState = merge({},state,{
					detail:{
						basic:{
							isFetch:true
						}
					}
				});
				return newState;
			};
		case GET_BASIC_DETAIL_SUCCESS:
			// TODO: 成功获取数据详情，停止通讯
			{
				let { dataSource } = others.payload;
				let basic = cloneDeep(state.detail.basic);
					Object.assign(basic,{
						dataSource,
						isFetch:false
					})
				let detail = cloneDeep(state.detail);
					Object.assign(detail,{
						basic
					});
				let newState = Object.assign({},state, {
					detail
				});		
				return newState;
			};
		case GET_BASIC_DETAIL_FAILURE:
			// TODO: 获取数据详情失败，返回错误消息，停止通讯
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					detail:{
						basic:{
							isFetch:false
						}
					},
					message:errorMessage
				});	
				return newState;
			};
		case SELECT_BASIC_DETAIL_PAGEINDEX:
			//活跃热力图翻页
			{
				let { currentPageIndex, pageIndex } = others.payload;
				
				return merge({},state,{
					detail:{
						basic:{
							pageIndex,
							currentPageIndex
						}
					}
				})
			};
		case SELECT_BASIC_DETAIL_CURRENT_PAGEINDEX:
			//活跃热力图翻页
			{
				let { currentPageIndex } = others.payload;
				
				return merge({},state,{
					detail:{
						basic:{
							currentPageIndex
						}
					}
				})
			};
		case GET_ACTIVE_DETAIL_REQUEST:
			// TODO: 请求数据详情
			{
				let newState = merge({},state,{
					detail:{
						active:{
							isFetch:true
						}
					}
				});
				return newState;
			};
		case GET_ACTIVE_DETAIL_SUCCESS:
			// TODO: 成功获取数据详情，停止通讯
			{
				let { dataSource } = others.payload;
				let active = cloneDeep(state.detail.active);
					Object.assign(active,{
						dataSource,
						isFetch:false
					})
				let detail = cloneDeep(state.detail);
					Object.assign(detail,{
						active
					});
				let newState = Object.assign({},state, {
					detail
				});		
				return newState;
			};
		case GET_ACTIVE_DETAIL_FAILURE:
			// TODO: 获取数据详情失败，返回错误消息，停止通讯
			{
				let { errorMessage } = others.payload;
				let newState = merge({},state,{
					detail:{
						active:{
							isFetch:false
						}
					},
					message:errorMessage
				});	
				return newState;
			};
		case SELECT_ACTIVE_DETAIL_PAGEINDEX:
			//活跃热力图翻页
			{
				let { pageIndex } = others.payload;

				return merge({},state,{
					detail:{
						active:{
							pageIndex
						}
					}
				});
			};
		case SELECT_ACTIVE_DETAIL_CURRENT_PAGEINDEX:
			//活跃热力图翻页
			{
				let { currentPageIndex } = others.payload;

				return merge({},state,{
					detail:{
						active:{
							currentPageIndex
						}
					}
				});
			};
		case RESET: 
			return INITIALIZE_STATE;
		default: 
			return state;
	}
};